<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/8/6
 * Time: 10:46
 */

namespace app\api\model;


use think\Model;

class ClassroomCatalogue extends Model
{
    /**
     * 获取课程节数
     * @param $classroom_id 课程ID
     * @return int|string
     */
    public function countNum($classroom_id){
        $num = $this->where('classroom_id', $classroom_id)->count('catalogue_id');
        return $num;
    }
    
    /**
     * 目录列表
     * @param $field  查询的字段
     * @param $classroom_id  课程ID
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function catalogueLists($field, $classroom_id){
        $lists = $this->field($field)
            ->where('classroom_id', $classroom_id)
            ->select();
        return $lists;
    }
    
    /**
     * 获取播放地址，当第一次加载页面时，显示本课程的第一节视频
     * @param string $catalogue_id  目录ID
     * @param $classroom_id  课程ID
     * @return array|false|mixed|\PDOStatement|string|Model
     */
    public function getUrl($classroom_id = '', $catalogue_id = '', $field = '*'){
        if($catalogue_id == ''){
            $url = $this
                ->field('min(catalogue_id) as catalogue_id, catalogue_url, catalogue_img, catalogue_title, catalogue_time')
                ->where('classroom_id', $classroom_id)
                ->find();
        }else{
            $url = $this->field($field)->where('catalogue_id', $catalogue_id)->find();
        }
        return $url;
    }
    
    /**
     * 根据课程ID ，获取该课程下所有目录的ID
     * @param $classroom_id  课程ID
     * @return array
     */
    public function catalogueIDList($classroom_id){
        $catalogueIds = $this->where('classroom_id', $classroom_id)->column('catalogue_id');
        return $catalogueIds;
    }
}